import {customRef} from 'vue'

function debounceRef(value: any = undefined, timeout: number = 0) {
  let timer: number | undefined;
  return customRef((track, trigger) => {
    return {
      get() {
        track();
        return value;
      },
      set(val) {
        clearTimeout(timer);
        timer =  setTimeout(() => {
          trigger();
          value = val;
        }, timeout)
      }
    }
  })
}

export {debounceRef}